import { fallbackRequestGet, fallbackRequestPost } from '@/utils/Request'

/**
 * 请求前缀，后面后台添加网关后需要前缀选择微服务
 */
const URL_PREFIX = 'middle-goods/'

/**
 * 根据id查询商品介绍内容信息
 * @param {String|Number} id 商品介绍内容编号
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const queryGoodProduceById = (id, successCallback = null, fullCallback = null) => {
  return fallbackRequestGet(URL_PREFIX + 'good-produce/queryGoodProduceById', { id }, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 根据id查询商品介绍内容信息
 * @param {Array<String|Number>} ids 商品介绍内容编号
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const queryGoodProduceByIds = (ids, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'good-produce/queryGoodProduceByIds', ids, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 根据商品id查询商品介绍内容信息
 * @param {String|Number} id 商品编号
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const queryGoodProduceByGoodId = (id, successCallback = null, fullCallback = null) => {
  return fallbackRequestGet(URL_PREFIX + 'good-produce/queryGoodProduceByGoodId', { id }, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 条件分页查询商品介绍信息
 * @param {PageDTO<GoodProduceQueryDTO>} pageDTO 条件分页查询数据
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const queryGoodProduceWrapperPage = (pageDTO, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'good-produce/queryGoodProduceWrapperPage', pageDTO, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 插入一条商品介绍内容信息，不能插入图片类型的商品介绍信息
 * @param {Array<GoodProduceInsertDTO>} list 插入数据
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const insertGoodProduce = (list, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'good-produce/insertGoodProduce', list, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 插入一条商品介绍内容信息，图片类型专用
 * @param {GoodProduceInsertDTO} goodProduceInsertDTO 插入数据
 * @param {Array} files 图片文件
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const insertGoodProduceWithFile = (goodProduceInsertDTO, files, successCallback = null, fullCallback = null) => {
  const config = {
    headers: { 'Content-Type': 'multipart/form-data' }
  }
  const formData = goodProduceInsertDTO.toFormData()
  files.forEach(item => {
    formData.append('files', item)
  })
  return fallbackRequestPost(URL_PREFIX + 'good-produce/insertGoodProduceWithFile', formData, config, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 根据商品介绍内容编号更改商品介绍内容信息
 * @param {Array<GoodProduceUpdateDTO>} list 插入数据
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const changeGoodProduceById = (list, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'good-produce/changeGoodProduceById', list, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 根据商品介绍内容编号移除商品介绍内容信息
 * @param {Array<Number|String>} ids 商品介绍内容编号
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const removeGoodProduceByIds = (ids, successCallback = null, fullCallback = null) => {
  return fallbackRequestPost(URL_PREFIX + 'good-produce/removeGoodProduceByIds', ids, {}, true, data => {
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}
